home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 08 - 1992 / 08.07 Nov⁄Dec 92 / Getting Started / EventMaster in C / EventMaster.c next >
Encoding:
C/C++ Source or Header  |  1992-10-28  |  3.4 KB  |  200 lines  |  [TEXT/KAHL]

  1. #include <Values.h>
  2.  
  3. #define kBaseResID            128
  4. #define kMoveToFront        (WindowPtr)-1L
  5. #define kSleep                MAXLONG
  6.  
  7. #define kRowHeight            14
  8. #define kFontSize            9
  9.  
  10. #define kMouseDown            1
  11. #define kMouseUp            2
  12. #define kKeyDown            3
  13. #define kAutoKey            4
  14.  
  15. /*************/
  16. /*  Globals  */
  17. /*************/
  18.  
  19. Boolean        gDone;
  20. short        gLastEvent = 0;
  21.  
  22. /***************/
  23. /*  Functions  */
  24. /***************/
  25.  
  26. void    ToolBoxInit( void );
  27. void    WindowInit( void );
  28. void    EventLoop( void );
  29. void    DoEvent( EventRecord *eventPtr );
  30. void    HandleMouseDown( EventRecord *eventPtr );
  31. void    DrawContents( void );
  32. void    SelectEvent( short eventType );
  33. void    DrawFrame( short eventType );
  34.  
  35. /******************************** main *********/
  36.  
  37. void    main( void )
  38. {
  39.     ToolBoxInit();
  40.     WindowInit();
  41.     
  42.     EventLoop();
  43. }
  44.  
  45. /*********************************** ToolBoxInit */
  46.  
  47. void    ToolBoxInit( void )
  48. {
  49.     InitGraf( &thePort );
  50.     InitFonts();
  51.     InitWindows();
  52.     InitMenus();
  53.     TEInit();
  54.     InitDialogs( nil );
  55.     InitCursor();
  56. }
  57.  
  58. /******************************** WindowInit *********/
  59.  
  60. void    WindowInit( void )
  61. {
  62.     WindowPtr    window;
  63.     
  64.     window = GetNewWindow( kBaseResID, nil, kMoveToFront );
  65.     
  66.     if ( window == nil )
  67.     {
  68.         SysBeep( 10 );    /*  Couldn’t load the WIND resource!!!  */
  69.         ExitToShell();
  70.     }
  71.     
  72.     SetPort( window );
  73.     TextSize( kFontSize );
  74.     
  75.     ShowWindow( window );
  76. }
  77.  
  78. /******************************** EventLoop *********/
  79.  
  80. void    EventLoop( void )
  81. {        
  82.     EventRecord        event;
  83.     
  84.     gDone = false;
  85.     while ( gDone == false )
  86.     {
  87.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  88.             DoEvent( &event );
  89.     }
  90. }
  91.  
  92. /************************************* DoEvent *********/
  93.  
  94. void    DoEvent( EventRecord *eventPtr )
  95. {
  96.     switch ( eventPtr->what )
  97.     {
  98.         case mouseDown:
  99.             SelectEvent( kMouseDown );
  100.             HandleMouseDown( eventPtr );
  101.             break;
  102.         case mouseUp:
  103.             SelectEvent( kMouseUp );
  104.             break;
  105.         case keyDown:
  106.             SelectEvent( kKeyDown );
  107.             break;
  108.         case autoKey:
  109.             SelectEvent( kAutoKey );
  110.             break;
  111.         case updateEvt:
  112.             BeginUpdate( (WindowPtr)eventPtr->message );
  113.             DrawContents();
  114.             EndUpdate( (WindowPtr)eventPtr->message );
  115.     }
  116. }
  117.  
  118. /******************************** HandleMouseDown *********/
  119.  
  120. void    HandleMouseDown( EventRecord *eventPtr )
  121. {
  122.     WindowPtr    window;
  123.     short        thePart;
  124.     
  125.     thePart = FindWindow( eventPtr->where, &window );
  126.     
  127.     if ( thePart == inGoAway )
  128.         gDone = true;
  129. }
  130.  
  131. /******************************** DrawContents *********/
  132.  
  133. void    DrawContents( void )
  134. {
  135.     short        i;
  136.     WindowPtr    window;
  137.     
  138.     window = FrontWindow();
  139.     
  140.     for ( i=1; i<=3; i++ )
  141.     {
  142.         MoveTo( 0, (kRowHeight * i) - 1 );
  143.         LineTo( window->portRect.right,
  144.                 (kRowHeight * i) - 1 );
  145.     }
  146.     
  147.     MoveTo( 4, 9 );
  148.     DrawString( "\pmouseDown" );
  149.     
  150.     MoveTo( 4, 9 + kRowHeight );
  151.     DrawString( "\pmouseUp" );
  152.     
  153.     MoveTo( 4, 9 + kRowHeight*2 );
  154.     DrawString( "\pkeyDown" );
  155.     
  156.     MoveTo( 4, 9 + kRowHeight*3 );
  157.     DrawString( "\pautoKey" );
  158.     
  159.     if ( gLastEvent != 0 )
  160.         DrawFrame( gLastEvent );
  161. }
  162.  
  163. /************************************* SelectEvent ********/
  164.  
  165. void    SelectEvent( short eventType )
  166. {
  167.     Rect        r;
  168.     WindowPtr    window;
  169.     
  170.     window = FrontWindow();
  171.     r = window->portRect;
  172.     
  173.     if ( gLastEvent != 0 )
  174.     {
  175.         ForeColor( whiteColor );
  176.         DrawFrame( gLastEvent );
  177.         ForeColor( blackColor );
  178.     }
  179.     
  180.     DrawFrame( eventType );
  181.     
  182.     gLastEvent = eventType;
  183. }
  184.  
  185. /************************************* DrawFrame *********/
  186.  
  187. void    DrawFrame( short eventType )
  188. {
  189.     Rect        r;
  190.     WindowPtr    window;
  191.     
  192.     window = FrontWindow();
  193.     r = window->portRect;
  194.     
  195.     r.top = kRowHeight * (eventType - 1);
  196.     r.bottom = r.top + kRowHeight - 1;
  197.     
  198.     FrameRect( &r );
  199. }
  200.